//拿到手机号码输入框
let phone1 = $('.phone')
let code = $('.third_row input')
let $span = $('.second_row span')
let username = $('.mima input').eq(0)
let password1 = $('.mima input').eq(1)
let password2 = $('.mima input').eq(2)
let regi = $('.forth_row button')
$('.phone').blur(function() {
        //正则验证手机号码为11位数
        let v = phone1.val()
        let r = /^1[3456789]\d{9}$/
        let res = r.test(v)
            //   console.log(res)
        if (!res) {
            phone1.val('')
            $(this).attr('placeholder', '请输入正确位数的手机号').html(phone1)
        }
    })
    //拿到验证码，并且添加点击事件，发起请求获得验证码
$span.click(function() {
        //判断手机号是不是注册了
        let url1 = 'http://47.96.224.206:8080/CandyFirst/user/validatePhoneNum'
        $.post(url1, {
            phoneNum: $('.phone').val()
        }, (res) => {
            console.log(res)
            if (res == "success") {
                let url = 'http://47.96.224.206:8080/CandyFirst/user/sendSms'
                $.post(url, {
                    phoneNum: $('.phone').val()
                }, function(res) {
                    console.log(res)
                    console.log(2)
                })
            } else if (res == 'fail') {
                phone1.val('')
                $(phone1).attr('placeholder', '手机号已经被注册了').html(phone1)
                console.log('用户已经注册过了')
            }
        })
    })
    //验证昵称,最多10个字符
username.blur(function() {
    let v = username.val()
    let r = /^[a-zA-Z0-9_]{3,16}$/
    let res = r.test(v)
    console.log(res)
    if (!res) {
        username.val('')
        $(this).attr('placeholder', '请输入(3,16)的字母或数字').html(username)
    }
})
password1.blur(function() {
    //正则密码,不能为空格并且位数为(6,16)
    let v = password1.val()
    let r = /^\S{6,16}$/
    res = r.test(v)
    if (!res) {
        password1.val('')
        password1.attr("type", "text")
        $(this).attr('placeholder', '密码不能为空格,且(6,16)').html(password1)
    }
    password1.attr("type", "password")
})
password2.blur(function() {
    if (password1.val() != password2.val()) {
        console.log("两次密码不一致")
    } else if (password1.val() == '' || password2.val() == '') {
        console.log("密码不能为空")
    }
})
regi.click(function() {
    let url = "http://47.96.224.206:8080/CandyFirst/user/regist"
    $.post(url, {
        phoneNum: $('.phone').val(),
        password: $('.mima input').eq(2).val(),
        name: $('.mima input').eq(0).val(),
        code: $('.third_row input').val()
    }, function(res) {
        if (res == 'success') {
            window.location.href = 'login.html'
        }
    })
})